﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using XNAGuiLib.Core.ActionHandling;
using XNAGuiLib.Core.Utilities;

namespace XNAGuiLib.Core.Components
{
    public class Button : GIComponent
    {

        #region Enumeration - ButtonStates
        public enum ButtonStates
        {
            None,
            HoverOnly,
            Default,
            All
        }
        #endregion


        #region Button Public Field - ButtonClickAction
        public MouseAction ButtonClickAction
        {
            get { return _mouseActions[0]; }
            set { _mouseActions[0] = value; }
        }
        #endregion

        #region Button Public Field - ButtonDownAction
        public MouseAction ButtonDownAction
        {
            get { return _mouseActions[1]; }
            set { _mouseActions[1] = value; }
        }
        #endregion


        #region Button Public Field - ButtonCommand
        protected string _buttonCommand;
        /// <summary>
        /// gets or sets the command to execute (at a 'click' of the button) on the target
        /// </summary>
        public string ButtonCommand
        {
            get { return this._buttonCommand; }
            set { this._buttonCommand = value; }
        }
        #endregion

        #region Button Public Field - ButtonTarget
        protected IAction _buttonTarget;
        /// <summary>
        /// gets or sets the buttons target (IAction reference) to execute the buttoncommand on
        /// </summary>
        public IAction ButtonTarget
        {
            get { return this._buttonTarget; }
            set { this._buttonTarget = value; }
        }
        #endregion



        public Button(Game game, ContentComponent normal, ContentComponent hover, ContentComponent clicked)
            : base(game)
        {
            // adds the normal button graphic at index 0
            normal.Visible = true;
            this.Add(normal);
            // adds the hover button graphic at index 2
            hover.Visible = false;
            this.Add(hover);
            // adds the clicked button graphic at index 2
            clicked.Visible = false;
            this.Add(clicked);

            // adds the buttons text at index 3
            // this._content.Add(normal);

            this._mouseActions[0] = new MouseAction(this, this, ActionCommand.BUTTON_CMD_CLICK, Enums.MouseKeys.Left, Enums.MouseKeyPositions.Pressed, KeyModifier.NONE);
            this._mouseActions[0].Enabled = true;

            this._mouseActions[1] = new MouseAction(this, this, ActionCommand.BUTTON_CMD_DOWN, Enums.MouseKeys.Left, Enums.MouseKeyPositions.Down, KeyModifier.NONE);
            this._mouseActions[1].Enabled = true;
        }


        #region Button Protected Method - EnableHoverOnly()
        protected void EnableHoverOnly()
        {
            foreach (Action action in _mouseActions)
            {
                action.Enabled = false;
            }
            _mouseActions[0].Enabled = true;
        }
        #endregion

        #region Button Protected Method - EnableDefault()
        protected void EnableDefault()
        {
            foreach (Action action in _mouseActions)
            {
                action.Enabled = false;
            }
            _mouseActions[0].Enabled = true;
            _mouseActions[1].Enabled = true;
        }
        #endregion
        
        #region Button Protected Method - EnableAll()
        protected void EnableAll()
        {
            foreach (Action action in _mouseActions)
            {
                action.Enabled = true;
            }
        }
        #endregion



        #region Button Public Method - Enable(ButtonStates state)
        public void Enable(ButtonStates state)
        {
            switch (state)
            {
                case ButtonStates.None:
                    this._enabled = false;
                    break;

                case ButtonStates.HoverOnly:
                    this._enabled = true;
                    this.EnableHoverOnly();
                    break;

                case ButtonStates.All:
                    this._enabled = true;
                    this.EnableAll();
                    break;

                default:
                    this._enabled = true;
                    this.EnableDefault();
                    break;

            }
        }
        #endregion


        #region Public Override Method - Update(GameTime gameTime)
        public override void Update(GameTime gameTime)
        {
            _ms = Mouse.GetState();

            if (this.Rectangle.Intersects(new Rectangle(_ms.X, _ms.Y, 1, 1)))
            {
                this._content[0].Visible = false;
                this._content[1].Visible = true;
                this._content[2].Visible = false;
            }
            else
            {
                this._content[0].Visible = true;
                this._content[1].Visible = false;
                this._content[2].Visible = false;
            }

            base.Update(gameTime);
        }
        #endregion

        #region Public Override Method - ExecuteCommand(string command)
        public override void ExecuteCommand(string command)
        {
            base.ExecuteCommand(command);

            if (command.Equals(ActionCommand.BUTTON_CMD_DOWN))
            {
                this._content[0].Visible = false;
                this._content[1].Visible = false;
                this._content[2].Visible = true;
            }
            else if (command.Equals(ActionCommand.BUTTON_CMD_CLICK))
            {
                this._content[0].Visible = false;
                this._content[1].Visible = false;
                this._content[2].Visible = true;

                if (_buttonTarget != null && _buttonCommand != null)
                    _buttonTarget.Execute(_buttonCommand);
            }
        }
        #endregion
    }
}